package com.genreate.util.xyzw;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 武将类
class Hero {
    String name;
    Position position;

    // 基础属性
    int attack;
    int maxHealth;
    int defense;
    int speed;

    // 特殊属性
    int damageIncrease;      // 增伤%
    int damageReduction;     // 减伤%
    int skillDamage;         // 技能伤害%
    int skillDamageReduction; // 技能减伤%
    int armorPenetration;    // 破甲%
    int armorPenetrationResist; // 破甲抵抗%
    int critRate;            // 暴击%
    int critResist;          // 暴击抵抗%
    int critDamage;          // 爆伤%
    int critDamageResist;    // 爆伤抵抗%
    int controlAccuracy;     // 控制命中%
    int controlResist;       // 免控%
    int block;               // 格挡%
    int precision;           // 精准%

    // 战斗状态
    int currentHealth;
    boolean isAlive = true;
    ControlStatus controlStatus = ControlStatus.NONE;
    List<PassiveSkill> passiveSkills = new ArrayList<>();
    Skill activeSkill;

    // 临时属性加成
    int tempAttackBonus = 0;
    int tempDefenseBonus = 0;
    int tempSpeedBonus = 0;

    public Hero(String name, Position position, int attack, int health, int defense, int speed) {
        this.name = name;
        this.position = position;
        this.attack = attack;
        this.maxHealth = health;
        this.currentHealth = health;
        this.defense = defense;
        this.speed = speed;
    }

    // 设置特殊属性
    public void setSpecialAttributes(int damageIncrease, int damageReduction,
                                     int skillDamage, int skillDamageReduction,
                                     int armorPenetration, int armorPenetrationResist,
                                     int critRate, int critResist,
                                     int critDamage, int critDamageResist,
                                     int controlAccuracy, int controlResist,
                                     int block, int precision) {
        this.damageIncrease = damageIncrease;
        this.damageReduction = damageReduction;
        this.skillDamage = skillDamage;
        this.skillDamageReduction = skillDamageReduction;
        this.armorPenetration = armorPenetration;
        this.armorPenetrationResist = armorPenetrationResist;
        this.critRate = critRate;
        this.critResist = critResist;
        this.critDamage = critDamage;
        this.critDamageResist = critDamageResist;
        this.controlAccuracy = controlAccuracy;
        this.controlResist = controlResist;
        this.block = block;
        this.precision = precision;
    }

    // 添加技能
    public void setActiveSkill(Skill skill) {
        this.activeSkill = skill;
    }

    public void addPassiveSkill(PassiveSkill skill) {
        this.passiveSkills.add(skill);
    }

    // 普通攻击
    public int normalAttack() {
        System.out.println(name + " 发动普通攻击!");
        int damage = attack + tempAttackBonus;

        // 触发被动技能
        for (PassiveSkill skill : passiveSkills) {
            skill.onAttack(this, null, damage);
        }

        return damage;
    }

    // 使用主动技能
    public void useActiveSkill(List<Hero> targets) {
        if (activeSkill != null && controlStatus != ControlStatus.SILENCE &&
                controlStatus != ControlStatus.STUN && controlStatus != ControlStatus.FREEZE) {
            System.out.println(name + " 发动主动技能: " + activeSkill.getName());
            activeSkill.execute(this, targets);
        }
    }

    // 受到伤害
    public void takeDamage(Hero attacker, int damage) {
        // 计算实际伤害
        int actualDamage = calculateDamage(attacker, damage);

        currentHealth -= actualDamage;
        System.out.println(name + " 受到 " + actualDamage + " 点伤害! (剩余血量: " + currentHealth + ")");

        // 触发被动技能
        for (PassiveSkill skill : passiveSkills) {
            skill.onDefend(this, attacker, actualDamage);
        }

        if (currentHealth <= 0) {
            isAlive = false;
            System.out.println(name + " 被击败了!");
        }
    }

    // 计算实际伤害
    private int calculateDamage(Hero attacker, int rawDamage) {
        // 基础伤害计算
        int damage = rawDamage * (100 + attacker.damageIncrease - damageReduction) / 100;

        // 破甲计算
        int effectiveArmorPen = Math.max(0, attacker.armorPenetration - armorPenetrationResist);
        int defenseAfterPen = defense * (100 - effectiveArmorPen) / 100;

        // 防御减伤
        damage -= defenseAfterPen / 2;
        if (damage < 1) {
            damage = 1;
        }

        // 格挡判断
        if (isBlocked(attacker)) {
            damage = damage * 60 / 100; // 格挡减少40%伤害
            System.out.println(name + " 格挡了部分伤害!");
        }

        return damage;
    }

    // 判断是否格挡
    private boolean isBlocked(Hero attacker) {
        Random rand = new Random();
        int blockChance = block - attacker.precision;
        return rand.nextInt(100) < Math.max(0, blockChance);
    }

    // 判断是否暴击
    public boolean isCriticalHit() {
        Random rand = new Random();
        int effectiveCritRate = Math.max(0, critRate - critResist);
        return rand.nextInt(100) < effectiveCritRate;
    }

    // 获取暴击伤害倍数
    public float getCritDamageMultiplier() {
        return 1.5f + (critDamage - critDamageResist) / 100f;
    }

    // 尝试施加控制效果
    public void applyControlEffect(ControlStatus effect, int accuracy) {
        if (controlStatus != ControlStatus.NONE) {
            return; // 已有控制效果
        }

        Random rand = new Random();
        int resistChance = controlResist - accuracy;
        if (rand.nextInt(100) >= Math.max(0, resistChance)) {
            controlStatus = effect;
            System.out.println(name + " 被" + effect.name() + "了!");
        }
    }

    // 清除控制效果
    public void clearControlEffects() {
        if (controlStatus != ControlStatus.NONE) {
            System.out.println(name + " 的" + controlStatus.name() + "效果解除了");
        }
        controlStatus = ControlStatus.NONE;
    }

    // 回合开始时的处理
    public void onRoundStart() {
        // 清除临时属性加成
        tempAttackBonus = 0;
        tempDefenseBonus = 0;
        tempSpeedBonus = 0;

        // 触发被动技能
        for (PassiveSkill skill : passiveSkills) {
            skill.onRoundStart(this);
        }
    }

    // 战斗开始时的处理
    public void onBattleStart() {
        // 触发被动技能
        for (PassiveSkill skill : passiveSkills) {
            skill.onBattleStart(this);
        }
    }
}
